/**
 * Copyright (c) 2017, 西安星沙网络科技-版权所有
 *
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.sstech.member.core.util;

import java.io.IOException;
import java.util.Set;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
import org.codehaus.jackson.annotate.JsonMethod;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.JavaType;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
 * json转换器
 * 
 * @author chenx
 * @date 2017年10月26日 下午4:03:43
 * @version 1.0
 */
public class JsonUtils {

	public final static void convert(Object json) {
		if (json instanceof JSONArray) {
			JSONArray arr = (JSONArray) json;
			for (Object obj : arr) {
				convert(obj);
			}
		} else if (json instanceof JSONObject) {
			JSONObject jo = (JSONObject) json;
			Set<String> keys = jo.keySet();
			String[] array = keys.toArray(new String[keys.size()]);
			for (String key : array) {
				Object value = jo.get(key);
				String[] key_strs = key.split("_");
				if (key_strs.length > 1) {
					StringBuilder sb = new StringBuilder();
					for (int i = 0; i < key_strs.length; i++) {
						String ks = key_strs[i];
						if (!"".equals(ks)) {
							if (i == 0) {
								sb.append(ks);
							} else {
								int c = ks.charAt(0);
								if (c >= 97 && c <= 122) {
									int v = c - 32;
									sb.append((char) v);
									if (ks.length() > 1) {
										sb.append(ks.substring(1));
									}
								} else {
									sb.append(ks);
								}
							}
						}
					}
					jo.remove(key);
					jo.put(sb.toString(), value);
				}
				convert(value);
			}
		}
	}

	public final static Object convert(String json) {
		Object obj = JSON.parse(json);
		convert(obj);
		return obj;
	}
	
	private static ObjectMapper mapper;  
	public static ObjectMapper getObjectMapper(){  
	    if(null == mapper){  
	        mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);  
	        mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);  
	    }  
	      
	    return mapper;  
	}  
	  
	   /**    
	    * 获取泛型的Collection Type   
	    * @param collectionClass 泛型的Collection    
	    * @param elementClasses 元素类    
	    * @return JavaType Java类型    
	    * @since 1.0    
	    */     
	     
	  
	 public static Object convertToCollection(String jsonString ,Class<?> collectionClass, Class<?>... elementClasses) throws JsonParseException, JsonMappingException, IOException{  
	     Object result = new Object();  
	     JavaType javaType = getObjectMapper().getTypeFactory().constructParametricType(collectionClass, elementClasses);  
	     result = getObjectMapper().readValue(jsonString, javaType);  
	       
	     return result;  
	 }   
}
